package com.readbook.action;


import com.readbook.service.JfscService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/jfsc")
public class jfsc {
    @Resource
    private JfscService jfscService;


    @RequestMapping("search_jfsc.html")
    @ResponseBody
    public Map<String, Object> search_jfsc(HttpServletRequest request, HttpServletResponse response) {
        String spname = "";
        if (request.getParameter("spname") != null) {
            spname = request.getParameter("spname").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("spname", spname);

        List<Map<String, Object>> list1 = jfscService.search_jfsc(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("updatestate.html")
    @ResponseBody
    public Map<String, Object> updatestate(HttpServletRequest request, HttpServletResponse response) {
        String spid = request.getParameter("spid").trim();
        String spstate = request.getParameter("spstate").trim();
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("spid", spid);
        gmap.put("spstate", spstate);
        Map<String, Object> getm = jfscService.updatestate(gmap);
        return getm;
    }


    /**
     * @author 牟明艳
     * @comments 添加商品
     * @time 2019年8月13日13:58:25
     */
    @RequestMapping("add_jfsc.html")
    public @ResponseBody
    Map<String, Object> add_jfsc(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String spid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("spid", spid);
        //商品名称
        String spname = "";
        if (req.getParameter("spname") != null) {
            spname = req.getParameter("spname").toString().trim();
        }
        gmap.put("spname", spname);

        //商品总数量
        String allnum = "";
        if (req.getParameter("allnum") != null) {
            allnum = req.getParameter("allnum").toString().trim();
        }
        gmap.put("allnum", allnum);


        //所需积分
        String needjf = "";
        if (req.getParameter("needjf") != null) {
            needjf = req.getParameter("needjf").toString().trim();
        }
        gmap.put("needjf", needjf);

        //商品图片
        String spurl = "";
        if (req.getParameter("spurl") != null) {
            spurl = req.getParameter("spurl").toString().trim();
        }
        gmap.put("spurl", spurl);

        //已兑换
        String ydh = "";
        if (req.getParameter("ydh") != null) {
            ydh = req.getParameter("ydh").toString().trim();
        }
        gmap.put("ydh", ydh);
        gmap.put("spstate", "0");

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = jfscService.add_jfsc(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 修改商品
     * @time 2019年8月13日14:04:57
     */
    @RequestMapping("edit_jfsc.html")
    public @ResponseBody
    Map<String, Object> edit_jfsc(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String spid = req.getParameter("spid");
        gmap.put("spid", spid);

        String spname = req.getParameter("spname");
        gmap.put("spname", spname);

        String allnum = req.getParameter("allnum");
        gmap.put("allnum", allnum);

        String needjf = req.getParameter("needjf");
        gmap.put("needjf", needjf);

        String spurl = req.getParameter("spurl");
        gmap.put("spurl", spurl);

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = jfscService.edit_jfsc(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    @RequestMapping("search_jfdhjl.html")
    @ResponseBody
    public Map<String, Object> search_jfdhjl(HttpServletRequest request, HttpServletResponse response) {
        String spid = "";
        if (request.getParameter("spid") != null) {
            spid = request.getParameter("spid").toString().trim();
        }

        String sname = "";
        if (request.getParameter("sname") != null) {
            sname = request.getParameter("sname").toString().trim();
        }

        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj").toString().trim();
        }

        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("spid", spid);
        gmap.put("sname", sname);
        gmap.put("nj", nj);
        gmap.put("bj", bj);

        List<Map<String, Object>> list1 = jfscService.search_jfdhjl(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 领取商品
     * @time 2019年8月13日14:04:57
     */
    @RequestMapping("lingqu_jfdhjl.html")
    public @ResponseBody
    Map<String, Object> lingqu_jfdhjl(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String dhjlid = req.getParameter("dhjlid");
        gmap.put("dhjlid", dhjlid);

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = jfscService.lingqu_jfdhjl(gmap);
        if (m > 0) {
            rmap.put("result", true);
            rmap.put("msg", "领取成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询积分商城商品
     * @time 2019年9月5日11:26:46
     */
    @RequestMapping("getShangpinAll.html")
    @ResponseBody
    public Map<String, Object> getShangpinAll(HttpServletRequest request, HttpServletResponse response) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("start", startn);
        gmap.put("end", endn);

        List<Map<String, Object>> list1 = jfscService.getShangpinAll(gmap);
        int totalnum = jfscService.countShangpinAll(gmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        rmap.put("totalnum", totalnum);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 获取我的积分与称号
     * @time 2019年9月24日10:28:04
     */
    @RequestMapping("getmyjfych.html")
    @ResponseBody
    public Map<String, Object> getmyjfych(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);
        List<Map<String, Object>> list1 = jfscService.getMyJfyCh(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        if (list1.size() > 0) {
            rmap = list1.get(0);
        }
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 新增积分兑换记录
     * @time 2019年9月24日15:27:39
     */
    @RequestMapping("addjfdhjl.html")
    public @ResponseBody
    Map<String, Object> addjfdhjl(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String dhjlid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("dhjlid", dhjlid);

        String usid = req.getParameter("usid");
        gmap.put("usid", usid);

        String sname = req.getParameter("sname");
        gmap.put("sname", sname);

        String nj = req.getParameter("nj");
        gmap.put("nj", nj);

        String bj = req.getParameter("bj");
        gmap.put("bj", bj);

        String spid = req.getParameter("spid");
        gmap.put("spid", spid);

        String usejf = req.getParameter("usejf");
        gmap.put("usejf", usejf);


        String sflq = req.getParameter("sflq");
        gmap.put("sflq", sflq);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("dhsj", df.format(new Date()));

        String dhsl = req.getParameter("dhsl");
        gmap.put("dhsl", dhsl);


        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = jfscService.addjfdhjl(gmap);
        if (m > 0) {
            gmap.put("ydh", Integer.parseInt(dhsl));
            //修改兑换数量
            jfscService.updateJfscYdh(gmap);
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


}
